package com.cloudspace.jindun.utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.dianhua.CallActivity;
import com.cloudspace.jindun.model.Person;

import static com.cloudspace.jindun.R.id.civil_callway_btn;

/**
 * Created by zengxianhua on 17/1/8.
 */

public class CallingUtils {
    public static void showCallMethodDialog(final Activity activity, final Person person){
        if (ObjUtil.isEmpty(activity) || ObjUtil.isEmpty(person)){
            return;
        }

        View view = LayoutInflater.from(activity).inflate(R.layout.call_method_dialog, null);

        Button civilBtn = (Button) view.findViewById(civil_callway_btn);
        Button innerBtn = (Button) view.findViewById(R.id.inner_callway_btn);
        Button traditionalBtn = (Button) view.findViewById(R.id.traditional_callway_btn);

        setButtonColor(civilBtn, !TextUtils.isEmpty(person.getSipAccount()));
        setButtonColor(innerBtn, !TextUtils.isEmpty(person.getSipAccount()));
        setButtonColor(traditionalBtn, !TextUtils.isEmpty(person.getPhoneNumber()));

        final MyDialog dialog = MMAlert.showAlert(activity, activity.getResources().getString(R.string.callway), view,
                activity.getResources().getString(R.string.app_cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        civilBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);

                sipCall(activity, person);
            }
        });

        innerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);

                sipCall(activity, person);
            }
        });

        traditionalBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);

                traditionalCall(activity, person);
            }
        });
    }

    private static void dismissDialog(MyDialog dialog) {
        if (dialog != null){
            dialog.dismiss();
            dialog = null;
        }
    }

    private static void setButtonColor(Button button, boolean enable){
        if (button == null){
            return;
        }

        button.setEnabled(enable);
        button.setTextColor(enable? Color.parseColor("#ffffff"):Color.parseColor("#666666"));
        button.setBackgroundColor(enable? Color.parseColor("#2AA1F5"):Color.parseColor("#E9E9E9"));
    }

    private static void traditionalCall(Activity activity, Person person) {
        if (activity != null && person!=null){
            if (TextUtils.isEmpty(person.getPhoneNumber())){
                ToastMaker.showToastShort(JindunApp.getInstance().getResources().getString(R.string.empty_phone_number));
                return;
            }

            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel://" + person.getPhoneNumber()));
            activity.startActivity(intent);
        }
    }

    private static  void sipCall(Activity activity, Person person) {
        if (ObjUtil.isEmpty(activity) || ObjUtil.isEmpty(person)){
            return;
        }

        Intent intent = new Intent(activity, CallActivity.class);

        Bundle bundle = new Bundle();
        bundle.putSerializable("person", person);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }
}
